package com.darren.euc.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.SystemParameter;
import com.darren.euc.service.SystemService;
import com.darren.euc.util.DateUtil;
import com.darren.euc.util.UpoadFileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * 通用上传接口
 * Created by hanji on 2018/4/8.
 */
@Controller
@RequestMapping("/commUpload")
public class FtpUploadAction{
    private static final Logger log = Logger.getLogger(FtpUploadAction.class);

    @Autowired
    private SystemService systemService;

    private static final String CONFIG_FILE_PATH = "CONFIG_FILE_PATH";
    /**
     * ftp通用上传接口
     * @param request
     * @return
     */
    @RequestMapping("ftpUpload")
    @ResponseBody
    public Object uploadResult(HttpServletRequest request){
        ResponseJSON responseJson = new ResponseJSON();
        String path = request.getParameter("path");
        if(StringUtils.isNotEmpty(path)) {
            List<SystemParameter> systemParameters = systemService.getAll();
            String real_path = "";
            for (SystemParameter systemParameter : systemParameters) {
                if(systemParameter.getLookupType().equals(path)){
                    real_path = systemParameter.getLookupValue();
                    break;
                }
            }
            if(StringUtils.isNotEmpty(real_path)) {
                String json = UpoadFileUtils.uploadFile(request, real_path);
                System.out.println(json);
                JSONObject jsonObject = JSON.parseObject(json);
                Integer error_code = jsonObject.getInteger("error_code");
                if(error_code != 200){
                    responseJson.setStatus(error_code);
                    responseJson.setMessage(jsonObject.getString("data"));
                }else{
                    responseJson.setStatus(200);
                    responseJson.setMessage("上传文件成功");
                }
            }else{
                responseJson.setStatus(400);
                responseJson.setMessage("没有找到此上传类型");
            }
        }else{
            responseJson.setStatus(405);
            responseJson.setMessage("缺少上传路径");
        }
        return responseJson;
    }

    /**
     * 获取上传的类别
     * @return
     */
    @RequestMapping(value = "uploadSelect")
    @ResponseBody
    public Object uploadSelect(){
        ResponseJSON responseJson = new ResponseJSON();
        responseJson.setStatus(200);
        List<SystemParameter> systemParameters = systemService.getAll();
        List<Map<String,String>> lists = new ArrayList<>();
        Map<String,String> map = new HashMap<>(16);
        for (SystemParameter systemParameter : systemParameters) {
            map = new HashMap<>(16);
            if(systemParameter.getLookupCat().equals(CONFIG_FILE_PATH)){
                map.put("value",systemParameter.getLookupType());
                map.put("label",systemParameter.getComment());
                lists.add(map);
            }
        }
        responseJson.setMessage("获取成功");
        responseJson.setData(lists);
        return responseJson;
    }
}
